
/*==========================================================================
	NameSpace:	undo
	Purpose: 	Basicaly we redirect undo to get owners for props
==========================================================================*/

if ( undo ) and ( SERVER ) then
	undo.WPP = {}
	
	
	undo._Create = undo.Create
	function undo.Create( Text )
		undo.WPP = {}
		undo.WPP.Entities = {}
		undo.WPP.Owner = nil
		return undo._Create(Text)
	end
	
	
	undo._AddEntity = undo.AddEntity
	function undo.AddEntity( Ent )
		if ( undo.WPP == nil ) then return end
		if ( !Ent ) then return end
		if ( !Ent:IsValid() ) then return end
		undo.WPP.Entities[ #undo.WPP.Entities ] = Ent
		return undo._AddEntity(Ent)
	end
	
	
	undo._SetPlayer = undo.SetPlayer
	function undo.SetPlayer( Player )
		if ( undo.WPP == nil ) then return end	
		if ( !Player ) or ( !Player:IsValid() ) then return end
		undo.WPP.Owner = Player
		return undo._SetPlayer( Player )
	end
	
	
	undo._Finish = undo.Finish
	function undo.Finish( NiceText )
		if ( undo.WPP == nil ) then return end
		if ( undo.WPP.Owner == nil ) then return end
		if ( !undo.WPP.Owner:IsValid() ) then return end
		
		for _,Entity in pairs( undo.WPP.Entities ) do
			hook.Call("WPP_SetOwner",GAMEMODE,Entity,undo.WPP.Owner)
		end
		
		return undo._Finish(NiceText)
	end
end


/*==========================================================================
	NameSpace:	cleanup
	Purpose: 	Basicaly we redirectcleanup to get owners for props
==========================================================================*/
if ( cleanup ) and ( SERVER ) then
	WPP.AddCleanUp = cleanup.Add
	function cleanup.Add(Player, Type, Entity)
		if ( Entity ) then
			if ( Player ) and ( Player:IsPlayer() ) and ( Entity:IsValid() ) then
		       hook.Call("WPP_SetOwner",GAMEMODE,Entity,Player)
		    end	
		end
	end
end

/*==========================================================================
	Hook:	WPP_SetOwner -> WPP
	Purpose: 	Basicaly we redirectcleanup to get owners for props
==========================================================================*/
hook.Add("WPP_SetOwner","WPP",function(Entity,Player)
	Entity:WPP_SetOwner( Player:UniqueID() )
	
	if ( SERVER ) then
		timer.Simple(0.5,function(Entity,Player) //Short delay required for clientside entity creation.
			umsg.Start("WPP_SO")
				umsg.Entity(Entity)
				umsg.Entity(Player)
			umsg.End()
		end,Entity,Player)
	end
	
end)
